package com.yuzhitong.shapi.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.palette.graphics.Palette;
import androidx.palette.graphics.Target;

import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.yuzhitong.shapi.R;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

/**
 * FileName: GlideUtil
 * Author: brsya
 * Date: 2020/12/3 10:29
 * Description: Glide工具类
 */
public class GlideUtil {
    private static RequestOptions defaultOptions;
    private static RequestOptions circleOptions;
    private static RequestOptions roundOptions;

    /**
     * 加载普通图片
     *
     * @param imageView 加载图片的ImageView
     * @param url       加载地址
     */
    public static void load(ImageView imageView, String url) {
        if (defaultOptions == null) {
            defaultOptions = new RequestOptions()
                    .error(R.drawable.bg_image_default)
                    .centerCrop();
        }
        Glide.with(imageView)
                .load(url)
                .apply(defaultOptions)
                .into(imageView);
    }

    /**
     * 加载普通图片
     *
     * @param imageView 加载图片的ImageView
     * @param url       加载地址
     */
    public static void load(ImageView imageView, int url) {
        if (defaultOptions == null) {
            defaultOptions = new RequestOptions()
                    .error(R.drawable.bg_image_default)
                    .centerCrop();
        }
        Glide.with(imageView)
                .load(url)
                .apply(defaultOptions)
                .into(imageView);
    }

    /**
     * 加载圆形图片
     *
     * @param imageView 加载图片的ImageView
     * @param url       加载地址
     */
    public static void loadCircle(ImageView imageView, String url) {
        if (circleOptions == null) {
            circleOptions = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.drawable.bg_image_default_circle)
                    .error(R.drawable.bg_image_default_circle)
                    .circleCrop();
        }
        Glide.with(imageView)
                .load(url)
                .apply(circleOptions)
                .into(imageView);
    }

    /**
     * 加载圆形图片
     *
     * @param imageView 加载图片的ImageView
     * @param url       加载地址
     */
    public static void loadCircle(ImageView imageView, int url) {
        if (circleOptions == null) {
            circleOptions = new RequestOptions()
                    .centerCrop()
                    .error(R.drawable.bg_image_default_circle)
                    .circleCrop();
        }
        Glide.with(imageView)
                .load(url)
                .apply(circleOptions)
                .into(imageView);
    }

    /**
     * 加载圆形图片
     *
     * @param imageView 加载图片的ImageView
     * @param url       加载地址
     */
    public static void loadCircle(ImageView imageView, String url, int defaultImg) {
        if (circleOptions == null) {
            circleOptions = new RequestOptions().centerCrop().circleCrop();
        }
        Glide.with(imageView)
                .load(url)
                .apply(circleOptions.placeholder(defaultImg))
                .into(imageView);
    }

    /**
     * 加载圆角图片
     *
     * @param imageView 加载图片的ImageView
     * @param url       加载地址
     * @param roundSize 圆角大小 单位dp
     */
    public static void loadRound(ImageView imageView, String url, int roundSize) {
        if (roundOptions == null) {
            roundOptions = new RequestOptions()
                    .error(R.drawable.bg_image_default)
                    .centerCrop();
        }
        roundOptions = roundOptions.optionalTransform(new BitmapRoundTransform(roundSize));
        Glide.with(imageView)
                .load(url)
                .apply(roundOptions)
                .into(imageView);
    }


    public static void loadBitmapColor(Context context, String url, OnBitmapColorListener onBitmapColorListener){
        Glide.with(context).asBitmap().load(url).into(new CustomTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                Palette.Filter filter = new Palette.Filter() {
                    @Override
                    public boolean isAllowed(int rgb, @NonNull float[] hsl) {
                        return true;
                    }
                };

                Target build = new Target.Builder()
                        .setMaximumLightness(.5f)
                        .build();

                new Palette.Builder(resource)
                        .addFilter(filter)
                        .addTarget(build)
                        .setRegion(0, 0, resource.getWidth(), resource.getHeight())
                        .generate(new Palette.PaletteAsyncListener() {
                            @Override
                            public void onGenerated(@Nullable Palette palette) {
                                if (palette != null) {
                                    int lightVibrantColor = palette.getDominantColor(0xFFFFFFFF);
                                    if (onBitmapColorListener != null) {
                                        onBitmapColorListener.onBitmapColor(lightVibrantColor);
                                    }
                                }
                            }
                        });

            }

            @Override
            public void onLoadCleared(@Nullable Drawable placeholder) {

            }
        });
    }


    public interface OnBitmapColorListener{
        void onBitmapColor(int color);
    }
}
